<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>Vue单页应用的基本配置 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Vue,">
  

  <meta name="description" content="1.1 路由配置 我们来看一下 CLI 给我们生成的 router.js 文件的配  /* router.js */import Vue from &apos;vue&apos;import Router from &apos;vue-router&apos;import Home from &apos;./views/Home.vue&apos; // 引入 Home 组件import About from &apos;./views/About.vue&apos; //">
<meta name="keywords" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue单页应用的基本配置">
<meta property="og:url" content="http://blog.poetries.top/2019/06/01/vue-single-page-config/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="1.1 路由配置 我们来看一下 CLI 给我们生成的 router.js 文件的配  /* router.js */import Vue from &apos;vue&apos;import Router from &apos;vue-router&apos;import Home from &apos;./views/Home.vue&apos; // 引入 Home 组件import About from &apos;./views/About.vue&apos; //">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2018/10/30/166c0985b8c2ae68">
<meta property="og:updated_time" content="2020-08-15T04:25:31.938Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue单页应用的基本配置">
<meta name="twitter:description" content="1.1 路由配置 我们来看一下 CLI 给我们生成的 router.js 文件的配  /* router.js */import Vue from &apos;vue&apos;import Router from &apos;vue-router&apos;import Home from &apos;./views/Home.vue&apos; // 引入 Home 组件import About from &apos;./views/About.vue&apos; //">
<meta name="twitter:image" content="https://user-gold-cdn.xitu.io/2018/10/30/166c0985b8c2ae68">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-路由配置"><span class="toc-text">1.1 路由配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-Vuex-配置"><span class="toc-text">1.2 Vuex 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-接口配置"><span class="toc-text">1.3 接口配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-公共设施配置"><span class="toc-text">1.4 公共设施配置</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-vue-single-page-config" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">Vue单页应用的基本配置</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2019.06.02</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.4k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 6分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h3 id="1-1-路由配置"><a href="#1-1-路由配置" class="headerlink" title="1.1 路由配置"></a>1.1 路由配置</h3><blockquote>
<p>我们来看一下 <code>CLI</code> 给我们生成的 <code>router.js</code> 文件的配</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* router.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">'vue-router'</span></span><br><span class="line"><span class="keyword">import</span> Home <span class="keyword">from</span> <span class="string">'./views/Home.vue'</span> <span class="comment">// 引入 Home 组件</span></span><br><span class="line"><span class="keyword">import</span> About <span class="keyword">from</span> <span class="string">'./views/About.vue'</span> <span class="comment">// 引入 About 组件</span></span><br><span class="line"></span><br><span class="line">Vue.use(Router) <span class="comment">// 注册路由</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;</span><br><span class="line">    routes: [&#123;</span><br><span class="line">        path: <span class="string">'/'</span>,</span><br><span class="line">        name: <span class="string">'home'</span>,</span><br><span class="line">        component: Home</span><br><span class="line">    &#125;, &#123;</span><br><span class="line">        path: <span class="string">'/about'</span>,</span><br><span class="line">        name: <span class="string">'about'</span>,</span><br><span class="line">        component: About</span><br><span class="line">    &#125;]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这份配置可以算是最基础的路由配置，有以下几点需要进行优化：</p>
</blockquote>
<ul>
<li>如果路由存在二级目录，需要添加 base 属性，否则默认为 <code>&quot;/&quot;</code></li>
<li>默认路由模式是 <code>hash</code> 模式，会携带 # 标记，与真实 <code>url</code>不符，可以改为 <code>history</code> 模式</li>
<li>页面组件没有进行按需加载，可以使用 <code>require.ensure()</code>来进行优化</li>
</ul>
<p><strong>下面是我们优化结束的代码：</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* router.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">'vue-router'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入 Home 组件</span></span><br><span class="line"><span class="keyword">const</span> Home = <span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">require</span>.ensure([<span class="string">'./views/Home.vue'</span>], () =&gt; &#123;</span><br><span class="line">        resolve(<span class="built_in">require</span>(<span class="string">'./views/Home.vue'</span>))</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入 About 组件</span></span><br><span class="line"><span class="keyword">const</span> About = <span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">require</span>.ensure([<span class="string">'./views/About.vue'</span>], () =&gt; &#123;</span><br><span class="line">        resolve(<span class="built_in">require</span>(<span class="string">'./views/About.vue'</span>))</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Vue.use(Router)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> base = <span class="string">`<span class="subst">$&#123;process.env.BASE_URL&#125;</span>`</span> <span class="comment">// 动态获取二级目录</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;</span><br><span class="line">    mode: <span class="string">'history'</span>,</span><br><span class="line">    base: base,</span><br><span class="line">    routes: [&#123;</span><br><span class="line">        path: <span class="string">'/'</span>,</span><br><span class="line">        name: <span class="string">'home'</span>,</span><br><span class="line">        component: Home</span><br><span class="line">    &#125;, &#123;</span><br><span class="line">        path: <span class="string">'/about'</span>,</span><br><span class="line">        name: <span class="string">'about'</span>,</span><br><span class="line">        component: About</span><br><span class="line">    &#125;]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>当然，除了使用 <code>require.ensure</code> 来拆分代码，<code>Vue Router</code> 官方文档还推荐使用动态 <code>import</code> 语法来进行代码分块，比如上述 <code>require.ensure</code> 代码可以修改为</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 引入 Home 组件</span></span><br><span class="line"><span class="keyword">const</span> Home = <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">import</span>(<span class="string">'./views/Home.vue'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 引入 About 组件</span></span><br><span class="line"><span class="keyword">const</span> About = <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">import</span>(<span class="string">'./views/About.vue'</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如果你想给拆分出的文件命名，可以尝试一下 webpack 提供的 Magic Comments（魔法注释）</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">const</span> Home = <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">import</span>(<span class="comment">/* webpackChunkName:'home'*/</span> <span class="string">'./views/Home.vue'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="1-2-Vuex-配置"><a href="#1-2-Vuex-配置" class="headerlink" title="1.2 Vuex 配置"></a>1.2 Vuex 配置</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    state: &#123;</span><br><span class="line"></span><br><span class="line">    &#125;,</span><br><span class="line">    mutations: &#123;</span><br><span class="line"></span><br><span class="line">    &#125;,</span><br><span class="line">    actions: &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>主要有 4 个核心点：<code>state</code>、<code>mutations</code>、<code>actions</code> 及 <code>getter</code></p>
</blockquote>
<p>这里我用一句话介绍它们之间的关系就是：<strong>我们可以通过 actions 异步提交 mutations 去 修改 state 的值并通过 getter 获取</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">└── store</span><br><span class="line">    ├── index.js          # 我们组装模块并导出 store 的地方</span><br><span class="line">    ├── actions.js        # 根级别的 action</span><br><span class="line">    ├── mutations.js      # 根级别的 mutation</span><br><span class="line">    └── modules</span><br><span class="line">        ├── moduleA.js    # A模块</span><br><span class="line">        └── moduleB.js    # B模块</span><br></pre></td></tr></table></figure>
<p><img src="https://user-gold-cdn.xitu.io/2018/10/30/166c0985b8c2ae68" alt></p>
<blockquote>
<p>与单个 <code>store.js</code> 文件不同的是，我们按模块进行了划分，每个模块中都可以包含自己 <code>4</code> 个核心功能。比如模块 <code>A</code> 中</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* moduleA.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> moduleA = &#123;</span><br><span class="line">    state: &#123; </span><br><span class="line">        text: <span class="string">'hello'</span></span><br><span class="line">    &#125;,</span><br><span class="line">    mutations: &#123;</span><br><span class="line">        addText (state, txt) &#123;</span><br><span class="line">            <span class="comment">// 这里的 `state` 对象是模块的局部状态</span></span><br><span class="line">            state.text += txt</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    </span><br><span class="line">    actions: &#123;</span><br><span class="line">        setText (&#123; commit &#125;) &#123;</span><br><span class="line">            commit(<span class="string">'addText'</span>, <span class="string">' world'</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    getters: &#123;</span><br><span class="line">        getText (state) &#123;</span><br><span class="line">            <span class="keyword">return</span> state.text + <span class="string">'!'</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> moduleA</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* index.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line"><span class="keyword">import</span> moduleA <span class="keyword">from</span> <span class="string">'./modules/moduleA'</span></span><br><span class="line"><span class="keyword">import</span> moduleB <span class="keyword">from</span> <span class="string">'./modules/moduleB'</span></span><br><span class="line"><span class="keyword">import</span> &#123; mutations &#125; <span class="keyword">from</span> <span class="string">'./mutations'</span></span><br><span class="line"><span class="keyword">import</span> actions <span class="keyword">from</span> <span class="string">'./actions'</span></span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store(&#123;</span><br><span class="line">    state: &#123;</span><br><span class="line">        groups: [<span class="number">1</span>]</span><br><span class="line">    &#125;,</span><br><span class="line">    modules: &#123;</span><br><span class="line">        moduleA, <span class="comment">// 引入 A 模块</span></span><br><span class="line">        moduleB, <span class="comment">// 引入 B 模块</span></span><br><span class="line">    &#125;,</span><br><span class="line">    actions, <span class="comment">// 根级别的 action</span></span><br><span class="line">    mutations, <span class="comment">// 根级别的 mutations</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 根级别的 getters</span></span><br><span class="line">    getters: &#123;</span><br><span class="line">        getGroups (state) &#123;</span><br><span class="line">            <span class="keyword">return</span> state.groups</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;   </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样项目中状态的模块划分就更加清晰，对应模块的状态我们只需要修改相应模块文件即可</p>
</blockquote>
<h3 id="1-3-接口配置"><a href="#1-3-接口配置" class="headerlink" title="1.3 接口配置"></a>1.3 接口配置</h3><blockquote>
<p>我们可以在 src 目录下新建 services 文件夹用于存放接口文件：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">└── src</span><br><span class="line">    └── services</span><br><span class="line">        ├── http.js      # 接口封装</span><br><span class="line">        ├── moduleA.js    # A模块接口</span><br><span class="line">        └── moduleB.js    # B模块接口</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* http.js */</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">'whatwg-fetch'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// HTTP 工具类</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Http</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">async</span> request(method, url, data) &#123;</span><br><span class="line">        <span class="keyword">const</span> param = &#123;</span><br><span class="line">            method: method,</span><br><span class="line">            headers: &#123;</span><br><span class="line">                <span class="string">'Content-Type'</span>: <span class="string">'application/json'</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (method === <span class="string">'GET'</span>) &#123;</span><br><span class="line">            url += <span class="keyword">this</span>.formatQuery(data)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            param[<span class="string">'body'</span>] = <span class="built_in">JSON</span>.stringify(data)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Tips.loading(); // 可调用 loading 组件</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> fetch(url, param).then(<span class="function"><span class="params">response</span> =&gt;</span> <span class="keyword">this</span>.isSuccess(response))</span><br><span class="line">                .then(<span class="function"><span class="params">response</span> =&gt;</span> &#123;</span><br><span class="line">                    <span class="keyword">return</span> response.json()</span><br><span class="line">            &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 判断请求是否成功</span></span><br><span class="line">    <span class="keyword">static</span> isSuccess(res) &#123;</span><br><span class="line">        <span class="keyword">if</span> (res.status &gt;= <span class="number">200</span> &amp;&amp; res.status &lt; <span class="number">300</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> res</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">this</span>.requestException(res)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 处理异常</span></span><br><span class="line">    <span class="keyword">static</span> requestException(res) &#123;</span><br><span class="line">        <span class="keyword">const</span> error = <span class="keyword">new</span> <span class="built_in">Error</span>(res.statusText)</span><br><span class="line"></span><br><span class="line">        error.response = res</span><br><span class="line"></span><br><span class="line">        <span class="keyword">throw</span> error</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// url处理</span></span><br><span class="line">    <span class="keyword">static</span> formatQuery(query) &#123;</span><br><span class="line">        <span class="keyword">let</span> params = [];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (query) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> item <span class="keyword">in</span> query) &#123;</span><br><span class="line">                <span class="keyword">let</span> vals = query[item];</span><br><span class="line">                <span class="keyword">if</span> (vals !== <span class="literal">undefined</span>) &#123;</span><br><span class="line">                    params.push(item + <span class="string">'='</span> + query[item])</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> params.length ? <span class="string">'?'</span> + params.join(<span class="string">'&amp;'</span>) : <span class="string">''</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 处理 get 请求</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">get</span>(url, data) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.request(<span class="string">'GET'</span>, url, data)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 处理 put 请求</span></span><br><span class="line">    <span class="keyword">static</span> put(url, data) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.request(<span class="string">'PUT'</span>, url, data)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 处理 post 请求</span></span><br><span class="line">    <span class="keyword">static</span> post(url, data) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.request(<span class="string">'POST'</span>, url, data)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 处理 patch 请求</span></span><br><span class="line">    <span class="keyword">static</span> patch(url, data) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.request(<span class="string">'PATCH'</span>, url, data)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 处理 delete 请求</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">delete</span>(url, data) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.request(<span class="string">'DELETE'</span>, url, data)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* moduleA.js */</span></span><br><span class="line"><span class="keyword">import</span> Http <span class="keyword">from</span> <span class="string">'./http'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取测试数据</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> getTestData = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> Http.get(<span class="string">'https://api.github.com/repos/octokit/octokit.rb'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>然后在项目页面中进行调用，会成功获取 <code>github</code> 返回的数据，但是一般我们在项目中配置接口的时候会直接省略项目 <code>url</code> 部分，比如</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* moduleA.js */</span></span><br><span class="line"><span class="keyword">import</span> Http <span class="keyword">from</span> <span class="string">'./http'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取测试数据</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> getTestData = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> Http.get(<span class="string">'/repos/octokit/octokit.rb'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这时候我们再次调用接口的时候会发现其调用地址为本地地址：<a href="http://127.0.0.1:8080/repos/octokit/octokit.rb，那么为了让其指向" target="_blank" rel="noopener">http://127.0.0.1:8080/repos/octokit/octokit.rb，那么为了让其指向</a> <a href="https://api.github.com，我们需要在" target="_blank" rel="noopener">https://api.github.com，我们需要在</a> vue.config.js 中进行 devServer 的配置</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* vue.config.js */</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    devServer: &#123;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">// string | Object 代理设置</span></span><br><span class="line">        proxy: &#123;</span><br><span class="line">        </span><br><span class="line">            <span class="comment">// 接口是 '/repos' 开头的才用代理</span></span><br><span class="line">            <span class="string">'/repos'</span>: &#123;</span><br><span class="line">                target: <span class="string">'https://api.github.com'</span>, <span class="comment">// 目标地址</span></span><br><span class="line">                changeOrigin: <span class="literal">true</span>, <span class="comment">// 是否改变源地址</span></span><br><span class="line">                <span class="comment">// pathRewrite: &#123;'^/api': ''&#125;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="1-4-公共设施配置"><a href="#1-4-公共设施配置" class="headerlink" title="1.4 公共设施配置"></a>1.4 公共设施配置</h3><blockquote>
<p>最后我们项目开发中肯定需要对一些公共的方法进行封装使用，这里我把它称之为公共设施，那么我们可以在 <code>src</code> 目录下建一个 <code>common</code> 文件夹来存放其配置文件</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">└── src</span><br><span class="line">    └── common</span><br><span class="line">        ├── index.js      # 公共配置入口</span><br><span class="line">        ├── validate.js   # 表单验证配置</span><br><span class="line">        └── other.js      # 其他配置</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在入口文件中我们可以向外暴露其他功能配置的模块，比如</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* index.js */</span></span><br><span class="line"><span class="keyword">import</span> Validate <span class="keyword">from</span> <span class="string">'./validate'</span></span><br><span class="line"><span class="keyword">import</span> Other <span class="keyword">from</span> <span class="string">'./other'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> &#123;</span><br><span class="line">    Validate,</span><br><span class="line">    Other,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2019/06/01/vue-cli3/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2019/06/01/vue-muti-page-config/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
